package com.opo5.logisticsmanagement.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.opo5.logisticsmanagement.common.R;
import com.opo5.logisticsmanagement.domain.Depot;
import com.opo5.logisticsmanagement.domain.Locator;
import com.opo5.logisticsmanagement.service.DepotService;
import com.opo5.logisticsmanagement.service.LocatorService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Slf4j
@RestController
@RequestMapping("/depots")
@CrossOrigin
public class DepotController {

    @Autowired
    private DepotService depotService;

    @Autowired
    private LocatorService locatorService;

    /**
     * 添加仓库信息
     * @param depot
     * @return
     */
    @PostMapping
    public R save(@RequestBody Depot depot){
        log.info("添加仓库信息");
        depotService.save(depot);
        return R.success("添加成功");
    }

    /**
     * 删除仓库信息
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public R delete(@PathVariable Integer id){
        log.info("删除仓库信息");
        LambdaQueryWrapper<Locator> lqw=new LambdaQueryWrapper<>();
        lqw.eq(Locator::getDepotId,id);
        List<Locator> locatorList = locatorService.list(lqw);
        if(CollectionUtils.isEmpty(locatorList)){
            depotService.removeById(id);
            return R.success("删除成功");
        }

        return R.success("仓库里还有东西，不要删我~~");
    }

    /**
     * 编辑仓库信息
     * @param depot
     * @return
     */
    @PutMapping
    public R update(@RequestBody Depot depot){
        log.info("修改仓库信息");
        depotService.updateById(depot);
        return R.success("编辑成功");
    }

    /**
     * 分页条件查询
     * @param currentPage
     * @param pageSize
     * @param depot
     * @return
     */
    @GetMapping("/{currentPage}/{pageSize}")
    public R getPage(@PathVariable Integer currentPage, @PathVariable Integer pageSize, Depot depot){
        log.info("分页条件查询");
        IPage<Depot> page = depotService.getPage(currentPage, pageSize, depot);
        return R.success(page);
    }

    /**
     * 获取所有仓库名
     * @return
     */
    @GetMapping("/getDepots")
    public R getDepots(){
        List<Depot> depots = depotService.list();
        List<Map<String,Object>> list=new ArrayList<Map<String,Object>>();
        for (Depot depot : depots) {
            Map<String,Object> map=new HashMap<String, Object>();
            map.put("depotName",depot.getDepotName());
            list.add(map);
        }
        return R.success(list);
    }

    /**
     * 根据仓库名获取库位信息
     * @param depotName
     * @return
     */
    @GetMapping("/getLocator/{depotName}")
    public R getLocatorNames(@PathVariable String depotName){
        List<Locator> locators = depotService.getLocatorNameByDeoptName(depotName);
        List<Map<String,Object>> list=new ArrayList<Map<String, Object>>();
        for (Locator locator : locators) {
            Map<String,Object> map=new HashMap<String,Object>();
            map.put("locatorName",locator.getLocatorName());
            list.add(map);
        }
        return R.success(list);
    }

}
